import math
import random
import sys
import re
import itertools  # 无限循环器


# R-1.1 n=mi ,i 是整数 isinstance(a,int)
def is_multiple(n, m):
    if (n == m) and (n == 0):
        return True
    if m == 0 and n != 0:
        return False
    a = n / m
    print(a)
    b = math.modf(a)
    return True if b != 0 else False


# R-1.2 判断偶数
def is_even(n):
    return True if ((n & 1) == 0) else False


# R-1.3  最小最大值
def minmax(data):
    ma = mi = data[0]
    for c in data:
        ma = c if c > ma else ma
        mi = mi if c > mi else c
    return mi, ma


# R-1.4 平方和
def pingfanghe(n):
    if not isinstance(n, int) or n < 1:
        return 0
    else:
        mys = 0
        for c in range(1, n + 1):
            # print(c)
            mys += c * c
        return mys


# R-1.5 平方和
def pingfanghe2(n):
    if not isinstance(n, int) or n < 1:
        return 0
    else:
        return sum([k * k for k in range(1, n + 1)])


# R-1.6 1-n奇数的平方和
def pingfangheodd(n):
    if not isinstance(n, int) or n < 1:
        return 0
    else:
        return sum([k * k for k in range(1, n + 1, 2)])


# R-1.7 return sum([k * k for k in range(1, n + 1, 2)])

# R-1.8 正索引与负索引的关系 j = k + len(list)

# R-1.9 生成 [50,60,70,80] 应为  a =  [ k for k in range(50,81,10)]
# R-1.10 生成 [8,6,4,2,0,-2,-4,-6,-8]  a = [k for k in  range(8,-9,-2)]
# R-1.11 生成 [1,2,4,8,16,32,64,128,256]  a = [1<<k for k in  range(0,9)]

# R-1.12 返回非空序列data的随机值
def choice(data):
    return data[random.randrange(len(data))]


# R-1.13 逆序
def reverse(data):
    # return [data[-1 - k] for k in range(0, len(data))]
    return data[::-1]


# R-1.14 乘积=奇数 的一对数，是否存在于list  ==> 存在两个奇数
def is_odd_pair(data):
    c = 0;
    for k in data:
        if k & 1 == 1:
            c += 1
            if c > 1:
                return True
    return False


# R-1.15 序列元素都不同, set 产生一个唯一的集合，与原数据长度比较就可以了
def is_dif_list(data):
    s = set(data)
    return len(s) == len(data)


# R-1.18 [0, 2, 6, 12, 20, 30, 42, 56, 72, 90]
# print([k * (k + 1) for k in range(0, 10)])

# R-1.19 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# print([chr(k) for k in range(ord('a'), ord('z') + 1)])

# R-1.20 洗牌函数
def shuffle(data):
    rdata = []
    if len(data) == 0:
        return rdata
    for i in range(len(data)):
        if len(data) < 1: break
        r = random.randint(0, len(data) - 1)
        rdata.append(data.pop(r))
    return rdata


# R-1.21 输入多行,直到EOFError == ctrl+d
def inputlines():
    data = []
    while True:
        try:
            data.append(input())
        except EOFError:
            print('输入反序的是:')
            list.reverse(data)
            print(data)
            break


def inputlines2():
    data = sys.stdin.readlines()
    data = [k.strip() for k in data]  # 去掉 \n
    list.reverse(data)
    print(data)


# R-1.22 两个数组的点积  同时遍历两个序列，使用打包函数 zip
def amb(a, b):
    data = [k * j for k, j in zip(a, b)]
    print(data)
    return data


# R-1.23 序列越界捕捉
def listoverflow(data):
    for x in range(0, len(data) + 5):
        try:
            print(data[x])
        except Exception as e:
            print(e)
            print("Don't try buffer overflow attacks in Python!")
            break


# R-1.24 字符元音个数
def aeiuo(data):
    c = 0
    for k in data:
        c += 1 if "aeiuo".__contains__(k) else 0
    return c


def aeiuo2(data):
    c = 0
    for k in "aeiuo":
        c += data.count(k)
    return c


# R-1.25 删除特定字符,如去掉 a or . or !
def strremove(data):
    return re.sub('[a\.!]', '', data)


# R-1.26 a+b=c or a=b-c or a*b=c
def abc(a, b, c):
    try:
        return (a + b == c) or (a == b - c) or (a * b == c)
    except Exception as e:
        print(e)
        return False


# R-1.28 欧几里得几何
def norm(v, p=2):
    d = [k ** p for k in v]
    print(d)
    return sum(d) ** (1 / p)


# R-1.29 字符所有组合
def alljoins(data):
    for k in itertools.permutations(data, len(data)):
        print(''.join(k))


# 交换字符数组下标为i和j对应的字符
def swap(ss, i, j):
    tmp = ss[i]
    ss[i] = ss[j]
    ss[j] = tmp


def permutation(ss, start):
    if ss == None or start < 0:
        return
    if start == len(ss) - 1:
        # 完成全排列后输出当前排列的字符串
        print(''.join(ss), end=' ')
    else:
        i = start
        while i < len(ss):
            # 交换start与i所在位置的字符
            swap(ss, start, i)
            # 固定第一个字符，对剩余的字符进行全排列
            permutation(ss, start + 1)
            # 还原start与i所在位置的字符
            swap(ss, start, i)
            i += 1


def permutation_transe(s):
    ss = list(s)
    permutation(ss, 0)


# R-1.30 被2除的次数
def timesdiv2(n):
    c = 0
    while n >= 2:
        n >>= 1
        c += 1
    print(c)
    return c

# print(is_multiple(2,1))
# print(is_even(2))
# print(is_even(3))
# print(minmax([1,2,3,4,5,-2,-3,-45.9,44.6,0,88,-108]))
# print(pingfanghe(6))
# print(pingfanghe2(10))
# print(pingfangheodd(3))
# a = [1,2,3,4,5]
# print(reverse(a))
# print(a)
# print(is_odd_pair([1,2,4,6]))
# print(is_dif_list([1,2,3,4,2]))
# print(shuffle([1, 2, 3, 4, 5, 6]))
# inputlines()
# inputlines2()
# amb([1,2,3],[4,5,6,8])
# listoverflow([1,2,3])
# print(aeiuo2("kaihua.yang"))
# print(strremove("kaihua.!!!!yang!"))
# print(abc(1,4,9))
# print(norm([1,2,3],3))
alljoins("catdog")
#permutation_transe("123")
# timesdiv2(32)